package com.baomidou.ant.service.impl;

import com.baomidou.ant.entity.PvpInfo;
import com.baomidou.ant.entity.QuestionInfo;
import com.baomidou.ant.entity.User;
import com.baomidou.ant.mapper.PvpInfoMapper;
import com.baomidou.ant.service.IPvpInfoService;
import com.baomidou.ant.service.IQuestionInfoService;
import com.baomidou.ant.service.IUserService;
import com.baomidou.ant.vo.Request.PvpChoiceInfoVo;
import com.baomidou.ant.vo.Request.RequestPvpListOfUserVo;
import com.baomidou.ant.vo.Response.ResponsePvpListOfUserVo;
import com.baomidou.ant.vo.Response.ResponsePvpQuestionVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jobob
 * @since 2021-03-24
 */
@Service(value = "pvpInfoService")
public class PvpInfoServiceImpl extends ServiceImpl<PvpInfoMapper, PvpInfo> implements IPvpInfoService {
    @Autowired
    private IQuestionInfoService questionInfoService;
    @Autowired
    private IUserService userService;

    @Override
    public IPage<ResponsePvpListOfUserVo> listOfUser(RequestPvpListOfUserVo requestPvpListOfUserVo) {
        IPage<PvpInfo> pvpInfoIPage=new Page<>(requestPvpListOfUserVo.getDisplayStart(),requestPvpListOfUserVo.getDisplayLength());
        QueryWrapper<PvpInfo> queryWrapper=new QueryWrapper<>();
        //被挑战者查询条件
        User user=userService.getById(requestPvpListOfUserVo.getUserId());
        queryWrapper.eq("user_name",user.getUsername()).or().eq("enemy_name",user.getUsername());
        //判断对战时间
        if(ObjectUtils.isNotEmpty(requestPvpListOfUserVo.getEnemy())){
//            queryWrapper.eq("user_name",requestPvpListOfUserVo.getEnemy()).or().eq("enemy_name",requestPvpListOfUserVo.getEnemy());
            queryWrapper.and(i->i.eq("enemy_name",requestPvpListOfUserVo.getEnemy())).or().eq("user_name",requestPvpListOfUserVo.getEnemy());
        }
        IPage<PvpInfo> iPage=page(pvpInfoIPage,queryWrapper);
        if(pvpInfoIPage.getRecords().size()<=0){
            return null;
        }
        //返回分页
        return iPage.convert(pvpInfo -> {
            ResponsePvpListOfUserVo responsePvpListOfUserVo=new ResponsePvpListOfUserVo();
            BeanUtils.copyProperties(pvpInfo,responsePvpListOfUserVo);
            responsePvpListOfUserVo.setEnemy(pvpInfo.getEnemyName());
            if (pvpInfo.getStatus().equals("1")) {
                responsePvpListOfUserVo.setStatus("进行中");
            }else{
                responsePvpListOfUserVo.setStatus("已结束");
            }
            //如果是被挑战者
            if(requestPvpListOfUserVo.getUserId().equals(pvpInfo.getEnemy())){
                if(responsePvpListOfUserVo.getResult().equals("败")){
                    responsePvpListOfUserVo.setResult("胜");
                }else if(responsePvpListOfUserVo.getResult().equals("胜")){
                    responsePvpListOfUserVo.setResult("败");
                }
                responsePvpListOfUserVo.setHispoints(pvpInfo.getMypoints());
                responsePvpListOfUserVo.setMypoints(pvpInfo.getHispoints());
                responsePvpListOfUserVo.setEnemy(pvpInfo.getUserName());
            }
            return responsePvpListOfUserVo;
        });
    }

    @Override
    public List<ResponsePvpQuestionVo> getQuestionOfPVP() {
        //定义三个属性，简单，中等，困难
        int easy=2;int normal=3;int hard=5;
        List<ResponsePvpQuestionVo> voList=new ArrayList<>();
        //先构造3个条件查询器,分别拿出简单中等和困难题(所有的)
        QueryWrapper<QuestionInfo> queryWrapperE=new QueryWrapper<>();
        queryWrapperE.eq("question_level","简单");
        List<QuestionInfo> easyQuestion=questionInfoService.list(queryWrapperE);
        QueryWrapper<QuestionInfo> queryWrapperN=new QueryWrapper<>();
        queryWrapperN.eq("question_level","中等");
        List<QuestionInfo> normalQuestion=questionInfoService.list(queryWrapperN);
        QueryWrapper<QuestionInfo> queryWrapperH=new QueryWrapper<>();
        queryWrapperH.eq("question_level","困难");
        List<QuestionInfo> hardQuestion=questionInfoService.list(queryWrapperH);
        //拿题
        int e = 0,n=0,h=0;
        int min = 1;
        int maxE = easyQuestion.size();
        int maxN=normalQuestion.size();
        int maxH=hardQuestion.size();
        Random random = new Random();
        while(e<=easy){
            int r=random.nextInt(maxE);
            if(r<min){
                r=r+maxE;
            }
            ResponsePvpQuestionVo vo=new ResponsePvpQuestionVo();
            BeanUtils.copyProperties(easyQuestion.get(r), vo);
            voList.add(vo);
            e++;
        }
        while(n<=normal){
            int r=random.nextInt(maxN);
            if(r<min){
                r=r+maxN;
            }
            ResponsePvpQuestionVo vo=new ResponsePvpQuestionVo();
            BeanUtils.copyProperties(normalQuestion.get(r), vo);
            voList.add(vo);
            n++;
        }
        while(h<=hard){
            int r=random.nextInt(maxH);
            if(r<min){
                r=r+maxH;
            }
            ResponsePvpQuestionVo vo=new ResponsePvpQuestionVo();
            BeanUtils.copyProperties(hardQuestion.get(r), vo);
            voList.add(vo);
            h++;
        }
        return voList;
    }

    @Override
    public boolean trueOrFalse(PvpChoiceInfoVo vo) {
        //根据题目id找到对应题目
        QuestionInfo questionInfo=questionInfoService.getById(vo.getQuestionId());
        //对比答案,返回结果
        return vo.getAnswer().equals(questionInfo.getRightItem());
    }
}
